﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Activity.Data;
using Activity.Helper;
using Activity.Model;

namespace Activity.API
{
    public class KMShopInvestigateHandler
    {
        public enum FirstType
        {
            默认 = 0,
            人 = 1,
            货 = 2,
            场 = 3,
            外部 = 4,
            其他因素 = 5,
        }

        public enum SecondType
        {
            默认 = 0,
            店铺管理 = 1,
            员工满意度 = 2,
        }

        public Messages Save(string code)
        {
            var message = new Messages
            {
                IsSuc = true,
                Code = 1,
            };
            try
            {
                var dc = new ActivityEntities();
                var obj = code.FromJson<List<KMShopInvestigateModel.CreatModel>>();
                if (obj == null || obj.Count <= 0)
                {

                    message.IsSuc = false;
                    message.Code = -1;
                    message.Message = "没有传入参数";
                    return message;
                }
                var shopName = obj.First().ShopName;
                var person = obj.First().Person;
                var station = obj.First().Station;
                if (string.IsNullOrEmpty(shopName))
                {

                    message.IsSuc = false;
                    message.Code = -1;
                    message.Message = "没有填写店铺名称";
                    return message;
                }
                if (string.IsNullOrEmpty(person))
                {

                    message.IsSuc = false;
                    message.Code = -1;
                    message.Message = "填写人不能为空";
                    return message;
                }
                if (string.IsNullOrEmpty(station))
                {

                    message.IsSuc = false;
                    message.Code = -1;
                    message.Message = "岗位不能为空";
                    return message;
                }
                if (dc.KM_Shop_Investigate.Any(d => d.ShopName.Equals(shopName) && d.Person.Equals(person) && d.Station.Equals(station)))
                {

                    message.IsSuc = false;
                    message.Code = -1;
                    message.Message = $"【{shopName}】-【{person}】-【{station}】已填写完毕";
                    return message;
                }
                foreach (var item in obj)
                {
                    var shop = new KM_Shop_Investigate
                    {
                        CreateDate = DateTime.Now,
                        ShopName = item.ShopName,
                        Person = item.Person,
                        Station = item.Station,
                        FirstType = (int) Enum.Parse(typeof(FirstType), item.FirstType),
                        SecondType = (int) Enum.Parse(typeof(SecondType), item.SecondType),
                        Element = item.Element,
                        Sort = item.Sort,
                        Other = item.Other,
                        Brand = item.Brand,
                        InYear = item.InYear,
                        ShopCount = item.ShopCount,
                        YearSaleAmount = item.YearSaleAmount,
                        Area = item.Area,
                        AddAreaName = item.AddAreaName,
                        AreaLevel = item.AreaLevel,
                        AreaCategory = item.AreaCategory,
                        GoodsCount = item.GoodsCount,
                        YoY=item.YoY,
                    };
                    dc.KM_Shop_Investigate.Add(shop);
                }
                dc.SaveChanges();
                message.Message = "保存成功！";
                return message;
            }
            catch (Exception ex)
            {
                message.IsSuc = false;
                message.Code = -1;
                message.Message = ex.Message;
                return message;
            }
        }

        public IList<IGrouping<string,KMShopInvestigateModel.ShowModel>> Total()
        {
            try
            {
                const string sql = @"SELECT  TABLE2.FirstType ,
        TABLE2.SecondType ,
        TABLE2.Element ,
        CASE WHEN TABLE4.SUM IS NULL THEN 0.0
             WHEN TABLE4.SUM = 0 THEN 0.0
             ELSE ROUND(CONVERT(FLOAT,TABLE2.Cou)*100 /CONVERT(FLOAT,TABLE4.SUM),2)
        END AS Accounted 
FROM    ( SELECT    [FirstType] ,
                    [SecondType] ,
                    [Element] ,
                    COUNT(1) AS Cou
          FROM      ( SELECT    [FirstType] ,
                                [SecondType] ,
                                [Element] ,
                                [Sort]
                      FROM      [dbo].[KM_Shop_Investigate]
					  WHERE FirstType!=5
                    ) TABLE1
          GROUP BY  [FirstType] ,
                    [SecondType] ,
                    [Element]
        ) TABLE2
        INNER JOIN ( SELECT [FirstType] ,
                            [SecondType] ,
                            COUNT(1) AS SUM
                     FROM   ( SELECT    [FirstType] ,
                                        [SecondType] ,
                                        [Element] ,
                                        [Sort]
                              FROM      [dbo].[KM_Shop_Investigate]
                            ) TABLE3
                     GROUP BY [FirstType] ,
                            [SecondType]
                   ) TABLE4 ON TABLE2.FirstType = TABLE4.FirstType
                               AND TABLE2.SecondType = TABLE4.SecondType
ORDER BY TABLE2.FirstType ,
        TABLE2.SecondType ,
        TABLE2.Cou DESC";
                var dc = new ActivityEntities();
                return
                    dc.Database.SqlQuery<KMShopInvestigateModel.ListModel>(sql)
                        .Select(d => new KMShopInvestigateModel.ShowModel
                        {
                            FirstType = d.FirstType,
                            SecondType = d.SecondType,
                            FirstTypeName = Enum.GetName(typeof(FirstType), d.FirstType),
                            SecondTypeName =
                                d.SecondType == 0
                                    ? Enum.GetName(typeof(FirstType), d.FirstType)
                                    : Enum.GetName(typeof(SecondType), d.SecondType),
                            Accounted = d.Accounted,
                            Element = d.Element
                        }).GroupBy(d => d.FirstType + "_" + d.SecondType).ToList();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }


    }
}
